/////////////////////////////////////////////////////////////////////////////
// Copyright(c) 2012-2015 purang All Rights Reserved
// Name:        NetworkServiceImpl.h
// Purpose:
// Author:      syy
// Modified by:
// Created:     2014/5/11 12:21
// RCS-ID:
// Licence:
/////////////////////////////////////////////////////////////////////////////
#ifndef _NETWORKSERVICEIMPL_H_
#define _NETWORKSERVICEIMPL_H_

#include <core/comdefs.h>
#include <core/common_types.h>
#include <core/observer/message_observer.h>
#include <core/system/atomic32.h>
#include <core/system/critical_section_wrapper.h>
#include <core/system/event_wrapper.h>

#include <map>

#include "../include/network_service.h"

#pragma comment(lib, "winmm.lib")

namespace qb {
class CSocketBase;
class CNetworkServiceImpl : public INetworkService {
 public:
  CNetworkServiceImpl();
  virtual ~CNetworkServiceImpl();

  STDMETHOD_(int32_t, CreateSocket)(THIS_ SocketType sock_type);
  STDMETHOD_(int32_t, DestorySocket)(THIS_ int32_t connect_id);
  STDMETHOD_(int32_t, Connect)
  (THIS_ int32_t connect_id, const char* ipAddress, uint16_t port);
  STDMETHOD_(int32_t, Disconnect)(THIS_ int32_t connect_id);
  STDMETHOD_(int32_t, DisconnectAll)(THIS_);
  STDMETHOD_(int32_t, Send)
  (THIS_ int32_t connect_id, const char* buff, uint32_t bytes);
  STDMETHOD_(int32_t, NotifyForceQuit)(THIS_ int32_t connect_id);
  STDMETHOD_(int32_t, NotifyDisconnect)(THIS_ int32_t connect_id);
  STDMETHOD_(int32_t, NotifyRecover)(THIS_ int32_t connect_id);
  STDMETHOD_(int32_t, RegisterNetworkServiceObserver)
  (THIS_ int32_t connect_id, INetworkServiceObserver* observer);
  STDMETHOD_(int32_t, DeRegisterNetworkServiceObserver)
  (THIS_ int32_t connect_id, INetworkServiceObserver* observer);
  STDMETHOD_(int32_t, RegisterNetworkEventObserver)
  (THIS_ INetworkEventObserver* observer);
  STDMETHOD_(int32_t, DeRegisterNetworkEventObserver)
  (THIS_ INetworkEventObserver* observer);
  STDMETHOD_(int32_t, SetProxy)
  (THIS_ int32_t proxy_type, const char* proxy_host, uint16_t proxy_port,
   const char* username, const char* password, const char* domain);
  STDMETHOD_(int32_t, ProxyTest)
  (THIS_ int32_t proxy_type, const char* dst_host, uint16_t dst_port,
   const char* proxy_host, uint16_t proxy_port, const char* username,
   const char* password, const char* domain);
  STDMETHOD_(uint32_t, GetTargetIP)(THIS_ int32_t connect_id);
  STDMETHOD_(uint16_t, GetTargetPort)(THIS_ int32_t connect_id);

 private:
  std::map<int32_t, CSocketBase*> m_sockets;
  BList<INetworkEventObserver*> m_observers;

  Atomic32 m_last_connect_id;
  CriticalSectionWrapper& m_critSect;
};

}  // namespace qb
#endif  // _NETWORKSERVICEIMPL_H_
